package com.mall.order.biz.handler;

import com.mall.order.mq.delay.producer.DelayOrderCancelProducer;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @Description: 利用mq发送延迟取消订单消息
 * @Date: 2019-09-17 23:14
 **/
@Component
@Slf4j
public class SendMessageHandler extends AbstractTransHandler {

    @Autowired
    DelayOrderCancelProducer producer;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        //  对每一个成功下单的订单，发送一个延迟消息
        // 1. 获取到订单id
        CreateOrderContext con = (CreateOrderContext) context;
        String orderId = con.getOrderId();
        // 2. 发送一个包含了订单id的延迟消息
        log.info("发送一个包含了订单id的延迟消息，orderId=" + orderId);
        return producer.sendDelayOrderCancelMessage(orderId);
    }
}
